package com.woniuxy.kaer.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.MD5Util;
import com.woniuxy.kaer.commons.util.OSSUtil;
import com.woniuxy.kaer.redis.util.RedisKey;
import com.woniuxy.kaer.system.dto.LoginManagerDTO;
import com.woniuxy.kaer.system.dto.ManagerDTO;
import com.woniuxy.kaer.system.mapper.ResourceMapper;
import com.woniuxy.kaer.system.mapper.RoleManagerMapper;
import com.woniuxy.kaer.system.mapper.RoleMapper;
import com.woniuxy.kaer.system.model.Manager;
import com.woniuxy.kaer.system.mapper.ManagerMapper;
import com.woniuxy.kaer.system.model.Role;
import com.woniuxy.kaer.system.model.RoleManager;
import com.woniuxy.kaer.system.param.LoginManagerParam;
import com.woniuxy.kaer.system.param.ManagerParam;
import com.woniuxy.kaer.system.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Resource
    private OSSUtil ossUtil;
    @Resource
    private ManagerMapper managerMapper;
    @Autowired
    private RoleManagerMapper roleManagerMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Qualifier("redisTemplate")
    @Resource
    private RedisTemplate<String, Object> redisObjectTemplate;


    @Override
    public void addManager(ManagerParam param) throws Exception {

        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_account", param.getManagerAccount());
        queryWrapper.eq("manager_mobile", param.getManagerMobile());
        Manager manager = managerMapper.selectOne(queryWrapper);
        if(manager!= null){
            throw new Exception("账号或手机号已存在");
        }


        Manager manager1 = new Manager();
        manager1.setManagerAccount(param.getManagerAccount());
        manager1.setManagerMobile(param.getManagerMobile());
        manager1.setManagerPass(MD5Util.md5(param.getManagerPass(),param.getManagerAccount()));

        managerMapper.insert(manager1);
        //添加角色管理关系
        RoleManager roleManager = new RoleManager();
        roleManager.setManagerId(manager1.getManagerId());
        roleManager.setRoleId(2);
        roleManagerMapper.insert(roleManager);

    }

    @Override
    public LoginManagerDTO login(LoginManagerParam param) throws Exception {
        System.out.println(param.getManagerAccount());
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_account", param.getManagerAccount());
        Manager manager = managerMapper.selectOne(queryWrapper);
        if(manager == null){
            throw new Exception("账号未注册");
        }
        if(!MD5Util.md5(param.getManagerPass(),param.getManagerAccount()).equals(manager.getManagerPass())){
            throw new Exception("密码错误");
        }
        //查询角色信息
        QueryWrapper<RoleManager> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("manager_id", manager.getManagerId());
        RoleManager roleManager = roleManagerMapper.selectOne(queryWrapper1);
        QueryWrapper<Role> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("role_id", roleManager.getRoleId());
        Role role = roleMapper.selectOne(queryWrapper2);
        if(role == null){
            throw new Exception("角色不存在");
        }

        //查询角色权限是否存在redis中
        List<String> resources = resourceMapper.selectResourceByRoleId(role.getRoleId());
        redisObjectTemplate.opsForValue().set(RedisKey.MANAGER_RESOURCE_LIST(manager.getManagerId()),resources);
        System.out.println(redisObjectTemplate.opsForValue().get(RedisKey.MANAGER_RESOURCE_LIST(manager.getManagerId())));


        LoginManagerDTO loginManagerDTO = BeanUtil.toBean(manager, LoginManagerDTO.class);
        loginManagerDTO.setRoleName(role.getRoleName());

        return loginManagerDTO;

    }

    @Override
    public List<ManagerDTO> findAllManager() throws Exception {
        ArrayList<ManagerDTO> managerDTOS = new ArrayList<>();
        //查询所有管理员信息
        List<Manager> managers = managerMapper.selectList(null);
        for (Manager manager : managers) {
            ManagerDTO dto = BeanUtil.toBean(manager, ManagerDTO.class);
            dto.setManagerAccount(manager.getManagerAccount());
            dto.setManagerMobile(manager.getManagerMobile());
            String roleNameByManagerId = managerMapper.selectRoleNameByManagerId(manager.getManagerId());
            dto.setRoleName(roleNameByManagerId);
            managerDTOS.add(dto);

        }
        return managerDTOS;
    }

}
